/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GraphsWindowPackage;
import AlertSystem.Window;
import javafx.scene.control.ScrollView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.LayoutInfo;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import GraphsWindowPackage.LabPackage.Lab;
import GraphsWindowPackage.LabPackage.LabView;
import GraphsWindowPackage.DocRecordPackage.DocRecord;
import java.lang.Boolean;
import java.lang.Void;
import GraphsWindowPackage.DocRecordPackage.DocRecordView;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollBarPolicy;
import javafx.scene.text.Text;
import GraphsWindowPackage.EntryView;
import GraphsWindowPackage.MedicationPackage.Medication;
import java.lang.System;
import GraphsWindowPackage.MedicationPackage.MedicationView;

/**
 * @author quang
 */

//def menuHeight = 500;

public class EntryGroupView extends Window {
    public var startShownTime: Number = 0;
    public var endShownTime: Number = 0;
    public var numPixPerTimeUnit: Number = 0;   //number of pixels per one time unit (day)    
    public var entryGroup: EntryGroup = null;

    ////////////////////////////////////////////////

    var shownEntries: Boolean[];// = for (i in [0..sizeof entryGroup.entries - 1 ]) {true};
    var entriesView: EntryView[];// = for (i in [0..sizeof entryGroup.entries - 1]) createEntryView(i);

    var isMenuHidden: Boolean = true;
    var areAllEntriesShown = true;

    function createEntryView(i: Integer): EntryView {
        if (entryGroup.entries[i] instanceof Lab)
            return LabView {
                var shown = true
                shown: bind shown
                translateX: groupLevelXOffset
                entry: entryGroup.entries[i]
                numPixPerTimeUnit: bind numPixPerTimeUnit //number of pixels per one time unit (day)
                startShownTime: bind startShownTime
                endShownTime: bind endShownTime
                onMouseClicked:function(e: MouseEvent): Void {
                    if (e.clickCount == 2) {
                        shown = false;
                        shownEntries[i] = false;
                    }
                }
//                onMouseDragged:function(e: MouseEvent): Void {
//                    shown = false;
//                    shownEntries[i] = false;
//                }
            }
        else if (entryGroup.entries[i] instanceof DocRecord)
            return DocRecordView {
                var shown = true
                shown: bind shown
                translateX: groupLevelXOffset
                entry: entryGroup.entries[i]
                numPixPerTimeUnit: bind numPixPerTimeUnit //number of pixels per one time unit (day)
                startShownTime: bind startShownTime
                endShownTime: bind endShownTime
                onMouseClicked:function(e: MouseEvent): Void {
                    if (e.clickCount == 2) {
                        shown = false;
                        shownEntries[i] = false;
                    }
                }
//                onMouseDragged:function(e: MouseEvent): Void {
//                    shown = false;
//                    shownEntries[i] = false;
//                }
            }
        else if (entryGroup.entries[i] instanceof Medication)
            return MedicationView {
                var shown = true
                shown: bind shown
                translateX: groupLevelXOffset
                entry: entryGroup.entries[i]
                numPixPerTimeUnit: bind numPixPerTimeUnit //number of pixels per one time unit (day)
                startShownTime: bind startShownTime
                endShownTime: bind endShownTime
                onMouseClicked:function(e: MouseEvent): Void {
                    if (e.clickCount == 2) {
                        shown = false;
                        shownEntries[i] = false;
                    }
                }
//                onMouseDragged:function(e: MouseEvent): Void {
//                    shown = false;
//                    shownEntries[i] = false;
//                }
            }
        else return null;
    }

    function createMenu (temp: Boolean[]) { //dummy paprameter 'temp' is for trigging this function
        var menuContent: Text[] = [];

        for (i in [0..sizeof shownEntries - 1]) {
            if (shownEntries[i] == false)
                insert Text {
                    content: "{entryGroup.entries[i].name}";
                    onMouseClicked:function(e: MouseEvent): Void {
                        if (e.clickCount == 2) {
                            shownEntries[i] = true;
                            entriesView[i] = createEntryView(i)
                        }
                    }
//                    onMouseDragged:function(e: MouseEvent): Void {
//                        shownEntries[i] = true;
//                        entriesView[i] = createEntryView(i)
//                    }
                } into menuContent;
         }

        return ScrollView {
            translateX: groupLevelXOffset
            opacity: bind if (isMenuHidden) 0.0 else 1.0
            layoutInfo: LayoutInfo {
                width: bind if (isMenuHidden) 0 else screenWidth * 0.25
                height: bind if (isMenuHidden) 0 else 100
            }
            hbarPolicy: ScrollBarPolicy.NEVER
            node: VBox {
                spacing: 5
                content: bind menuContent
            }            
        }
    }

    public override function create(): Node {
        shownEntries = for (i in [0..sizeof entryGroup.entries - 1 ]) {true};
        entriesView = for (i in [0..sizeof entryGroup.entries - 1]) { this.createEntryView(i) };

        var menu = bind this.createMenu(shownEntries);

        def selectButton = Button {
            text: "[...]"
            onMouseClicked: function(e: MouseEvent): Void {
                isMenuHidden = not isMenuHidden;
            }
        }
        
        def groupNameView = Text {
                font: font2
                content: if (entryGroup.entries[0] instanceof Lab) '  {entryGroup.name}\n'
                            else '{entryGroup.name}\n'
                onMouseClicked: function(e: MouseEvent): Void {                    
                    for (i in [0..sizeof shownEntries - 1]) {
                        shownEntries[i] = not areAllEntriesShown;
                        if (areAllEntriesShown) entriesView[i] = EntryView{}
                        else entriesView[i] = createEntryView(i);
                    }
                    areAllEntriesShown = not areAllEntriesShown;
                }
            };
            
        return VBox {
            translateX: groupLevelXOffset
            content: bind [
                HBox {
                    spacing: 5
                    content: bind [ groupNameView, selectButton ]
                },
                menu,
                entriesView
            ]
        }
    }

}
